﻿using System;

namespace RexToy.UI.Controls
{
    public class ListItem : ListViewItem
    {
        public override bool Clickable
        {
            get { return true; }
        }

        public override Palette Palette
        {
            get
            {
                return __palette;
            }
            set
            {
                this.SetItemClass(ButtonStatus.None);
                this.__palette = value;
                this.SetItemClass(ButtonStatus.Up);
            }
        }

        private string __text;
        public string Text
        {
            get { return __text; }
            set
            {
                this.__text = value;
                var dom = this.__domNode.SearchByName("content");
                dom._element.InnerHTML = value;
            }
        }

        private bool __enable = true;

        public bool Enable
        {
            get { return __enable; }
            set
            {
                if (value)
                {
                    this.SetItemClass(ButtonStatus.Up);
                    this.__domNode.RemoveClass("ui-disabled");
                }
                else
                {
                    this.SetItemClass(ButtonStatus.Up);
                    this.__domNode.AppendClass("ui-disabled");
                }

                this.__enable = value;
            }
        }

        private object __instance;
        public object Instance
        {
            get { return __instance; }
        }

        private string __iconName = "ui-icon-arrow-r";
        public string IconName
        {
            get { return __iconName; }
            set
            {
                var dom = this.__domNode.SearchByName("icon");
                dom.ReplaceClass(this.__iconName, value);
                this.__iconName = value;
            }
        }

        private bool __showCount;
        public bool ShowCount
        {
            get { return __showCount; }
            set
            {
                var countDom = this.__domNode.SearchByName("count");
                countDom.GetStyle().Display = (value ? System.Html.DOM.CSS.Display.InlineBlock : System.Html.DOM.CSS.Display.None);
                __showCount = value;
            }
        }


        private int __count;
        public int Count
        {
            get { return __count; }
            set
            {
                var countDom = this.__domNode.SearchByName("count");
                this.__count = value;
                countDom._element.InnerHTML = value.ToString();
            }
        }

        public string[] events = new string[] { "onclick" };

        public ListItem(string text, object instance)
        {
            this.__palette = Palette.C;
            this.__instance = instance;
            this.__text = text;
            this.__domNode = RexToy.UI.Controls.DomNode.Create(new DomNodeCreateOptions()
            {
                tag = Tag.Li,
                className = "ui-btn ui-btn-icon-right ui-li ui-li-has-count",
                items = new DomNodeCreateOptions[]
                {
                    new DomNodeCreateOptions()
                    {
                        tag= Tag.Div,
                        className= "ui-btn-inner ui-li",
                        items= new DomNodeCreateOptions[]
                        {
                            new DomNodeCreateOptions()
                            {
                                tag= Tag.Div,
                                className= "ui-btn-text",
                                items= new DomNodeCreateOptions[]
                                {
                                    new DomNodeCreateOptions()
                                    {
                                        tag= Tag.A,
                                        hashName= "content",
                                        className= "ui-link-inherit",
                                        innerHTML= this.__text,
                                        items= new DomNodeCreateOptions[]
                                        {
                                            new DomNodeCreateOptions()
                                            {
                                                tag= Tag.Img,
                                                hashName= "img",
                                                className= "ui-li-icon ui-li-thumb"
                                            },
                                            new DomNodeCreateOptions()
                                            {
                                                tag= Tag.Span,
                                                hashName= "count",
                                                className= "ui-li-count ui-btn-up-c ui-btn-corner-all",
                                                innerHTML= this.__count.ToString()
                                            }
                                        }
                                    }
                                }
                            },
                            new DomNodeCreateOptions()
                            {
                                tag= Tag.Span,
                                hashName= "icon",
                                className= "ui-icon ui-icon-arrow-r"
                            }
                        }
                    }
                }
            });
            this.__domNode.AttachEvent(DomEvent.OnMouseOut, new Action(this.OnMouseNormal).Bind(this));
            this.__domNode.AttachEvent(DomEvent.OnMouseDown, new Action(this.OnMouseDown).Bind(this));
            this.__domNode.AttachEvent(DomEvent.OnMouseOver, new Action(this.OnMouseOver).Bind(this));
            this.__domNode.AttachEvent(DomEvent.OnMouseUp, new Action(this.OnMouseNormal).Bind(this));
            this.__domNode.AttachEvent(DomEvent.OnClick, new Action(this.OnClick).Bind(this));

            this.SetItemClass(ButtonStatus.Up);

            this.ShowCount = false;
            this.ShowImg(false);
        }

        public void ShowImg(bool show)
        {
            var dom = this.__domNode.SearchByName("img");
            if (show)
            {
                this.__domNode.AppendClass("ui-li-has-icon");
                dom.GetStyle().Display = System.Html.DOM.CSS.Display.Block;
            }
            else
            {
                this.__domNode.RemoveClass("ui-li-has-icon");
                dom.GetStyle().Display = System.Html.DOM.CSS.Display.None;
            }
        }

        public void SetImgSrc(string srcUrl)
        {
            var dom = this.__domNode.SearchByName("img");
            dom.SetAttribute("src", srcUrl);
        }

        private void SetItemClass(ButtonStatus status)
        {
            if (this.__enable == true)
            {
                ButtonStyleHelper.SetDomNodeClass(this.__domNode, status, this.__palette);
            }
        }

        private void OnMouseNormal()
        {
            this.SetItemClass(ButtonStatus.Up);
        }

        private void OnMouseDown()
        {
            this.SetItemClass(ButtonStatus.Down);
        }

        private void OnMouseOver()
        {
            this.SetItemClass(ButtonStatus.Hover);
        }

        private void OnClick()
        {
            this.__FireEvent("onclick");
        }
    }
}
